﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Space_Conquest 
{
    class UnitManager :IRender, IMotion, IInput
    {

        #region Constructors

        public UnitManager(GameEngine parent)
        {

            _parent = parent;

        }

        #endregion 

        #region Private Members

        //private List<Texture> lTexture= new List<Texture>();
        private List<Unit> _units = new List<Unit>();
        private GameEngine _parent;

        private List<Unit> _pendingCreations = new List<Unit>();
        private List<Unit> _pendingDeletions = new List<Unit>();

        #endregion

        #region Properties

        public List<Unit> Units
        {
            get
            {
                return _units;
            }
        }

        public GameEngine Parent
        {
            get
            {
                return _parent;
            }
        }


        #endregion

        #region Methods

        /// <summary>
        /// Add Units
        /// </summary>
        /// <param name="newObject"></param>
        public void AddUnit(Unit unit)
        {
            _pendingCreations.Add(unit);
        }

        /// <summary>
        /// Removes Units
        /// </summary>
        /// <param name="unit"></param>
        public void RemoveUnit(Unit unit)
        {
            _pendingDeletions.Add(unit);
        }

        /// <summary>
        /// add texture
        /// </summary>
        /// <param name="newObject"></param>


        /// <summary>
        /// The simple render function
        /// </summary>
        public void Render()
        {

            if (_units != null)
            {

                foreach (Unit u in _units)
                {
                    u.Render();
                }

            }

        }

        /// <summary>
        /// The simple motion function
        /// </summary>
        public void Motion()
        {

            // Do motion on all units
            foreach (Unit u in _units)
            {
                u.Motion();
            }

            // Remove units from main list that died during this motion
            while (_pendingDeletions.Count > 0)
            {

                if (_pendingDeletions[0].Owner != null)
                    _pendingDeletions[0].Owner.MyUnits.Remove(_pendingDeletions[0]);
                
                _units.Remove(_pendingDeletions[0]);
                _pendingDeletions.RemoveAt(0);


            }

            // Add units that were created during this motion
            while (_pendingCreations.Count > 0)
            {

                if (_pendingCreations[0].Owner != null)
                    _pendingCreations[0].Owner.MyUnits.Add(_pendingCreations[0]);

                _units.Add(_pendingCreations[0]);
                _pendingCreations.RemoveAt(0);


            }

        }


        /// <summary>
        /// Mouse button is pressed down
        /// </summary>
        /// <param name="button"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public bool MouseDown(MouseButtons button, int x, int y)
        {
            return false;
        }

        /// <summary>
        /// Mouse buttons is let up
        /// </summary>
        /// <param name="button"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public bool MouseUp(MouseButtons button, int x, int y)
        {
            return false;
        }

        /// <summary>
        /// Mouse cursor is moved on screen
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public bool MouseMove(int x, int y)
        {
            return false;
        }

        /// <summary>
        /// A key is pressed down
        /// </summary>
        /// <param name="key"></param>
        /// <param name="shift"></param>
        public bool KeyDown(Keys key, bool shift)
        {
            return false;
        }

        /// <summary>
        /// A key is let up
        /// </summary>
        /// <param name="key"></param>
        /// <param name="shift"></param>
        public bool KeyUp(Keys key, bool shift)
        {
            return false;
        }

        #endregion

    }
}
